package zkCli;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;

import java.io.IOException;
import java.util.List;

/**
 * Created by 小泽玛丽罗 on 2016/11/12.
 */
public class ZookeeperOperator extends AbstractZookeeper {

    public void  create(String path,byte[] datas) throws KeeperException, InterruptedException {
        this.zooKeeper.create(path,datas, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    public List<String> getChild(String path) throws KeeperException, InterruptedException {
        List<String> list=zooKeeper.getChildren(path,false);
        return  list;
    }
    public  byte[] getData(String path) throws KeeperException, InterruptedException {
        byte[] data=zooKeeper.getData(path,false,null);
        return data;
    }

    public static void main(String[] args) throws InterruptedException {
        ZookeeperOperator zkOperator=new ZookeeperOperator();
        try {
            zkOperator.connect("192.168.80.128");
            byte[] data=new byte[]{'a','b','c','d'};
//            zkOperator.create("/root",null);
           /* System.out.println(zkOperator.getData("/root"));
            zkOperator.create("/root/child1",data);
            System.out.println(zkOperator.getData("/root/child1"));
            zkOperator.create("/root/child2",data);
            System.out.println(zkOperator.getData("/root/child2"));*/
            String zktest="ZooKeeper的Java API测试";
//            zkOperator.create("/root/child3", zktest.getBytes());
            String child3Date=new String(zkOperator.getData("/root/child3"));
            System.out.println("child3的节点信息:"+child3Date);
            List<String> childs=zkOperator.getChild("/root");
            for (String child:childs){
                System.out.println(child);
            }
            System.out.println("节点孩子信息:");
            zkOperator.getChild("/root");

            zkOperator.close();
        } catch (IOException e) {
            e.printStackTrace();
            zkOperator.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
            zkOperator.close();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
}
